package com.jie.service.impl;

import com.jie.dto.VideoDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.entity.Video;
import com.jie.mapper.VideoMapper;
import com.jie.service.VideoService;
import com.jie.store.context.UploadStrategyContext;
import com.jie.utils.BeanCopyUtils;
import com.jie.vo.ConditionVO;
import com.jie.vo.PageResult;
import com.jie.vo.VideoVO;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 视频服务实现类
 * </p>
 *
 * @author zwq
 * @since 2022-01-04
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    @Autowired
    private UploadStrategyContext uploadStrategyContext;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createVideo(VideoVO videoVO){
        //上传视频到minio中并返回预览视频url
//        String videoUrl = uploadStrategyContext.executeUploadStrategy(videoVO.getMultipartFile());
        //后续这里会接入文件上传来定义视频地址路径 现在默认
        Video video = Video.builder()
                .sportName(videoVO.getSportName())
                .sportInfo(videoVO.getSportInfo())
                .sportVideo(videoVO.getMultipartFile())
                .sportClass(videoVO.getSportClass())
                .consumable(videoVO.getConsumable())
                .build();
        return this.save(video);
    }

    @Override
    public Integer deleteById(Integer id) {
        //后续会删除视频资源再删除这条数据 删除视频可以采用异步推送mq去
        this.baseMapper.deleteById(id);
        //删除minio的资源

        return id;
    }

    @Override
    public PageResult<VideoDTO> listVideos(ConditionVO conditionVO) {
        Page<Video> page = new Page<>(conditionVO.getCurrent(), conditionVO.getSize());
        // 查询日志列表
        Page<Video> videoPage = this.page(page, new LambdaQueryWrapper<Video>()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Video::getSportName, conditionVO.getKeywords())
                .or()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Video::getSportInfo, conditionVO.getKeywords())
                .orderByDesc(Video::getId));
        List<VideoDTO> operationLogDTOList = BeanCopyUtils.copyList(videoPage.getRecords(), VideoDTO.class);
        return new PageResult<>(operationLogDTOList, (int) videoPage.getSize());
    }
}